/// Author:             Joe Audette
/// Created:            2006-01-20
/// Last Modified:      2010-06-05

using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web.UI;
using mojoPortal.Web.Framework;

namespace mojoPortal.Web
{
    
    public partial class layout : MasterPage
    {
        private int leftModuleCount = 0;
        private int centerModuleCount = 0;
        private int rightModuleCount = 0;
        private SiteSettings siteSettings;
        private PageSettings currentPage = null;
        private SiteMapDataSource siteMapDataSource = null;
        private SiteMapNode rootNode = null;
        protected string SkinBaseUrl = string.Empty;

        
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current == null) { return; }

            siteSettings = CacheHelper.GetCurrentSiteSettings();

            SkinBaseUrl = SiteUtils.GetSkinBaseUrl(Page);

            if (Page is CmsPage) { currentPage = CacheHelper.GetCurrentPage(); }
            
            if (siteSettings == null) return;

            siteMapDataSource = (SiteMapDataSource)this.FindControl("SiteMapData");
            if(siteMapDataSource == null){ return;}

            siteMapDataSource.SiteMapProvider
                    = "mojosite" + siteSettings.SiteId.ToInvariantString();

            try
            {
            rootNode = siteMapDataSource.Provider.RootNode;
            }
            catch(HttpException)
            {
                return;
            }

            SetupLayout();
        }


        /// <summary>
        /// Count items in each of the 3 columns to determine what css class to assign to center and whether to hide side columns.
        /// This gives us automatic adjustment of column layout from 1 to 3 columns for the main layout.
        /// </summary>
        private void SetupLayout()
        {
            // Count menus if they exist within a content pane and are visible
            CountVisibleMenus();

            CountContentInstances();

            // Set css classes based on count of items in each column panel
            divLeft.Visible = (leftModuleCount > 0);
            divRight.Visible = (rightModuleCount > 0);

            if((divLeft.Visible)&&(!divRight.Visible))
            {
                divLeft.CssClass = "art-layout-cell art-sidebar1 leftside left2column";
            }

            if ((divRight.Visible) && (!divLeft.Visible))
            {
                divRight.CssClass = "art-layout-cell art-sidebar2 rightside right2column";
            }

            divCenter.CssClass =
                divLeft.Visible
                    ? (divRight.Visible ? "art-layout-cell art-content-narrow center-rightandleftmargins cmszone" : "art-layout-cell art-content center-leftmargin cmszone")
                    : (divRight.Visible ? "art-layout-cell art-content center-rightmargin cmszone" : "art-layout-cell art-content-wide center-nomargins cmszone");

            divLeft.CssClass += " cmszone";
            divRight.CssClass += " cmszone";

            // these are optional panels that may exist in some skins
            // but are not part of the automatic column layout scheme
            Control alt = this.FindControl("divAlt1");
            if ((alt != null) && (alt is Panel))
            {
                ((Panel)alt).CssClass += " cmszone";
            }

            alt = this.FindControl("divAlt2");
            if ((alt != null) && (alt is Panel))
            {
                ((Panel)alt).CssClass += " cmszone";
            }

            alt = this.FindControl("divAltContent2");
            if ((alt != null) && (alt is Panel))
            {
                ((Panel)alt).CssClass += " cmszone";
            }

            
        }

        private void CountContentInstances()
        {
            if ((Page is CmsPage) && (currentPage != null))
            {
                foreach (Module module in currentPage.Modules)
                {
                    if ((module.ControlSource == "Modules/LoginModule.ascx") && (Request.IsAuthenticated)) { continue; }

                    if (ModuleIsVisible(module))
                    {
                        if (StringHelper.IsCaseInsensitiveMatch(module.PaneName, "leftpane"))
                        {
                            leftModuleCount++;
                        }

                        if (StringHelper.IsCaseInsensitiveMatch(module.PaneName, "rightpane"))
                        {
                            rightModuleCount++;
                        }

                        if (StringHelper.IsCaseInsensitiveMatch(module.PaneName, "contentpane"))
                        {
                            centerModuleCount++;
                        }
                    }
                }

                // this is to make room for ModuleWrapper or custom usercontrols if they exsits anywhere in left or right
                foreach (Control c in divRight.Controls)
                {
                    if (c is mojoUserControl) { rightModuleCount++; }
                }

                foreach (Control c in divLeft.Controls)
                {
                    if (c is mojoUserControl) { leftModuleCount++; }
                }

            }

            

        }

        private void CountVisibleMenus()
        {
            // Count menus if they exist within a content pane and are visible
            if ((SiteMenu1 != null) && SiteMenu1.Visible)
            {
                // printable view skin doesn't have a menu so it is null there
                if (SiteMenu1.Parent.ID == "divLeft") leftModuleCount++;
                if (SiteMenu1.Parent.ID == "divRight") rightModuleCount++;
            }

            Control c = this.FindControl("PageMenu1");
            if (
                (c != null)
                && (c.Visible)
                )
            {
                PageMenuControl p = (PageMenuControl)c;
                if ((!p.IsSubMenu)||(SiteUtils.TopPageHasChildren(rootNode, p.StartingNodeOffset)))
                {
                    if (c.Parent.ID == "divLeft") leftModuleCount++;
                    if (c.Parent.ID == "divRight") rightModuleCount++;
                }

            }

            c = this.FindControl("PageMenu2");
            if (
                (c != null)
                && (c.Visible)
                )
            {
                PageMenuControl p = (PageMenuControl)c;
                if (SiteUtils.TopPageHasChildren(rootNode, p.StartingNodeOffset))
                {
                    if (c.Parent.ID == "divLeft") leftModuleCount++;
                    if (c.Parent.ID == "divRight") rightModuleCount++;
                }
            }

            c = this.FindControl("PageMenu3");
            if (
                (c != null)
                && (c.Visible)
                )
            {
                PageMenuControl p = (PageMenuControl)c;
                if (SiteUtils.TopPageHasChildren(rootNode, p.StartingNodeOffset))
                {
                    if (c.Parent.ID == "divLeft") leftModuleCount++;
                    if (c.Parent.ID == "divRight") rightModuleCount++;
                }
            }

            c = this.FindControl("pnlMenu");
            if ((c != null) && (c.Parent.ID == "divLeft")) leftModuleCount++;

            c = this.FindControl("StyleSheetCombiner");
            if ((c != null) && (c is StyleSheetCombiner))
            {
                StyleSheetCombiner style = c as StyleSheetCombiner;
                if (style.AlwaysShowLeftColumn) { leftModuleCount++; }
                if (style.AlwaysShowRightColumn) { rightModuleCount++; }
            }

           

        }

        private bool ModuleIsVisible(Module module)
        {
            if ((module.HideFromAuthenticated) && (Request.IsAuthenticated)) { return false; }
            if ((module.HideFromUnauthenticated) && (!Request.IsAuthenticated)) { return false; }

            return true;
        }

       
    }
}
